<?php

namespace App\Http\Controllers\Admins\Appointment;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\Appointment;
use App\Models\OperationalLogs;
use App\Models\Setting;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AppointmentController extends BaseAdminController
{
    /**
     * 预约列表
     * @param Request $request
     * @return array|Application|Factory|View
     *
     */
    public function index(Request $request){
        $condition = $request->get('condition','username');
        if($request->isMethod('post')){//dd($request->all());
            # 要执行的代码
            $query = Appointment::query()->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
            $start_at = $request->get('start_at','');
            if($start_at){
                Log::info('$start_at:'.$start_at);
            }
            $end_at = $request->get('end_at','');
            if($start_at){
                Log::info('$end_at:'.$end_at);
            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $result[] = Appointment::getDataByID($val['id']);
                }
            }
//            echo $offset+1;
            return ret([
                'total'=>$total,
                'data'=>$result,
//                'page'=>$offset+1
            ]);
        }
        $admin_id = $request->get('admin_id');
        $subtitle = '预约管理';
        $compact = compact('subtitle','admin_id');
        return view('v1.admin.appointment.index',$compact);
    }


    /**
     * 预约须知
     *
     * @param Request $request
     * @return Application|Factory|View|RedirectResponse
     */
    public function notice(Request $request){
        $key = 'appointment_notice';
        if($request->isMethod('post')){
            $value = $request->get('content','');
            if(!$value){
                return $this->retError('预约须知内容不能为空',$request);
            }
            $logs = [
                'admin_id'=>$request->get('admin_id'),
                'operate_menu'=>OperationalLogs::$operateItem['appointment_manage'],
                'operate_item'=>OperationalLogs::$operate['appointment_notice_setting'],
                'operate_content'=>OperationalLogs::$operate['appointment_notice_setting'],
            ];
            DB::beginTransaction();
            $where = [
                'key'=>$key
            ];
            try {
                Setting::query()->where($where)->update([
                    'value'=>$value,
                ]);
                #添加操作日志
                OperationalLogs::AddOperationalLogs($logs);
                DB::commit();
                return $this->retSuccess();
//                return AdminRet('操作成功!');
            } catch (\Exception $exception) {
                DB::rollBack();
                return $this->retError($exception->getMessage());
            }
        }
        $subtitle = '预约须知';
        $content = Setting::getDataByKey($key);
        $params = [
            'url'=>'',
            'success'=>'success'
        ];
        $compact = compact('subtitle','content','params');
        return view('v1.admin.appointment.notice',$compact);
    }

    /**
     * 预约核销
     *
     * @param Request $request
     * @param $id
     * @return array|RedirectResponse
     */
    public function verify(Request $request,$id){
        $model = Appointment::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }
        $app_status = Appointment::query()->where($where)->value('status');
        if($app_status == 2){
            return AdminRet('操作成功!');
        }
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['appointment_manage'],
            'operate_item'=>'预约ID为：('.$id.')',
            'operate_content'=>OperationalLogs::$operate['appointment_status_change'].'为：已核销',
        ];
        DB::beginTransaction();
        try {
            Appointment::query()->where($where)->update([
                'status'=>2,
            ]);
            #添加操作日志
            OperationalLogs::AddOperationalLogs($logs);
            DB::commit();
            return AdminRet('操作成功!');
        } catch (\Exception $exception) {
            DB::rollBack();
            return AdminRetError($exception->getMessage());
        }
    }
}
